package com.polaris.live.popup

import android.content.Context
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.StringUtils
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.core.BottomPopupView
import com.polaris.live.R
import com.polaris.live.adapter.recharge.BuyGameBeansAdapter
import com.polaris.live.common.cacha.UserManager
import com.polaris.live.common.constant.CommonConst
import com.polaris.live.common.ext.dp
import com.polaris.live.common.ext.setOnSingleClickListener
import com.polaris.live.common.mvvm.viewmodel.BaseViewModel
import com.polaris.live.common.network.requestLoading
import com.polaris.live.common.util.DebounceHelper
import com.polaris.live.common.util.NavigationBarUtils
import com.polaris.live.common.util.ToastGravity
import com.polaris.live.databinding.PopupExchangeGameBinding
import com.polaris.live.dialog.GameCustomizeDialog
import com.polaris.live.im.manager.ImNoticeManager
import com.polaris.live.net.liveService
import com.polaris.live.resp.back_resp.PartyGameCoinExchangePriceResp
import com.polaris.live.resp.go_resp.GameCoinBean
import com.polaris.live.translation.R.string

/**
 * ExchangeGamePopup
 *
 * @author Created by 半仙 on 2024/3/13/013 17:04
 */
class ExchangeGamePopup(
    context: Context,
    private val mViewModel: BaseViewModel,
    private val mData: PartyGameCoinExchangePriceResp,
) : BottomPopupView(context) {

    override fun getImplLayoutId() = R.layout.popup_exchange_game

    private val mAdapter by lazy { BuyGameBeansAdapter() }

    private lateinit var mBinding: PopupExchangeGameBinding
    override fun onCreate() {
        super.onCreate()
        mBinding = PopupExchangeGameBinding.bind(popupImplView)
        mBinding.gamePopupRecycler.apply {
            adapter = mAdapter
            layoutManager = LinearLayoutManager(context)
            itemAnimator = null
        }
        initListener()
        initObserver()
        Glide.with(context).load(R.drawable.img_bliuee)
            .transform(RoundedCorners(12.dp))
            .into(mBinding.ivBackImg)

        val lp = mBinding.bottom.layoutParams as ViewGroup.LayoutParams
        lp.height = NavigationBarUtils.getNavigationBarHeight(context)
        mBinding.bottom.layoutParams = lp
    }

    private fun initObserver() {
        ImNoticeManager.observeGameAccountChange(this) {
            setGameCoin()
        }
        mAdapter.setList(mData.prices)
        setGameCoin()
    }

    private fun initListener() {
        mBinding.tvCustomRedemption.setOnSingleClickListener {
            XPopup.Builder(context)
                .hasShadowBg(true)
                .isDestroyOnDismiss(true)
                .asCustom(GameCustomizeDialog(context, mViewModel))
                .show()
        }
        mBinding.tvBuyDiamond.setOnSingleClickListener {
            GlobalPopupManager.showRechargePopup(context, mViewModel, isNeedBottom = true)
        }
        mAdapter.setOnItemChildClickListener { _, view, position ->
            DebounceHelper.singleClick {
                val item = mAdapter.getItemOrNull(position) ?: return@singleClick
                when (view.id) {
                    R.id.ll_game_coin_back -> {
                        buyGameCoin(item)
                    }
                }
            }
        }
    }

    private fun setGameCoin() {
        mBinding.userCoin.text = UserManager.getUser().account?.userGameCoins?.toString() ?: "0"
    }

    private fun buyGameCoin(coin: Long) {
        mViewModel.requestLoading(
            { liveService.gameCoinExchange(GameCoinBean(coin)) }, {
                ToastGravity.showGravity(StringUtils.getString(string.game_coin_success))
            }, {
                if (it.errCode == CommonConst.ErrorCode.SENDGIFT) {
                    GlobalPopupManager.showRechargePopup(context, mViewModel, isNeedBottom = true)
                }



                ToastGravity.showGravity(it.errorMsg)
            }, waitTime = 0
        )
    }
}